<?php

namespace Apps\AppApi\Method\Note;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\AccountBook;


/**
 * 记账记录列表
 * @author      545
 * @date        2016年3月4日
 * @version     1.1.2
 * @copyright   Copyright 2015 meelier.com
 */
class AbList extends BaseMethod
{
    
    private $success = 0;
    private $id = 0;

    public function run()
    {
        //验证是否登录
        if(false == $this->verifyUserAuth()){
            return false;
        }
        $page = intval($this->getDataItem('page',1));
        
        $limit = $this->getConfig()->limit;
        $offset = ($page-1)*$limit;
        
        $userid = $this->getUserAuth()->userId;
    	//获取记一笔列表
    	$data = AccountBook::query()
        ->columns(['ab_id', 'ab_cashnum','ab_cardnum','ab_type','addtime','i.inorout_id','i.inorout_title','ii.inorout_title as childtitle','m.member_name','ab_cashnum+ab_cardnum as sumnum'])
        ->leftJoin('Apps\Common\Models\Members','Apps\Common\Models\AccountBook.ab_memberid=m.member_id','m')
        ->leftJoin('Apps\Common\Models\Inorout','Apps\Common\Models\AccountBook.ab_innoroutid=i.inorout_id','i')
        ->leftJoin('Apps\Common\Models\Inorout','Apps\Common\Models\AccountBook.child_inoroutid=ii.inorout_id','ii')
        ->where('Apps\Common\Models\AccountBook.user_id = :uid:')
        ->bind(['uid'=>$userid])
        ->orderBy('Apps\Common\Models\AccountBook.addtime DESC,Apps\Common\Models\AccountBook.ab_id DESC')
        ->limit($limit,$offset)
        ->execute()
        ->toArray();
    	
//     	foreach ($data as $k=>$v){
//     	    $data[$k]['addtime'] = datte
//     	}
    	
    	//统计本月收入
    	$inornum = AccountBook::query()
    	->columns(['sum(ab_cashnum)+sum(ab_cardnum) as inornum'])
    	->where("user_id = :uid: AND ab_type = 1 AND date_format(Apps\Common\Models\AccountBook.addtime,'%Y%m') = date_format(curdate(),'%Y%m')")
    	->bind(['uid'=>$userid])
    	->execute()->getFirst();
    	//统计本月支出
    	$outnum = AccountBook::query()
    	->columns(['sum(ab_cashnum)+sum(ab_cardnum) as outnum'])
    	->where("user_id = :uid: AND ab_type = 2 AND date_format(Apps\Common\Models\AccountBook.addtime,'%Y%m') = date_format(curdate(),'%Y%m')")
    	->bind(['uid'=>$userid])
    	->execute()->getFirst();
    	$list['ablist'] = $data;
    	$list['inornum'] = $inornum->inornum;
    	$list['outnum'] = $outnum->outnum;
        $this->setResult($list);
        
    }

}